package com.erp.warehouse.clouderp_warehouse.controller;

import com.erp.warehouse.clouderp_warehouse.pojo.allot.AllotCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.allot.AllotModify;
import com.erp.warehouse.clouderp_warehouse.service.allot.AllotService;
import com.erp.warehouse.clouderp_warehouse.util.UniqueOrderCode;
import org.comple.erp.pojo.ResultStatus;
import org.comple.erp.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 2025/11/10 8:54
 * author: 向远洋
 */
@RestController
@RequestMapping("/allot")
public class AllotController {
    @Autowired
    AllotService allotService;
    @Autowired
    UniqueOrderCode uniqueOrderCode;
    @Value("${auth.staffId}")
    String staffId;
    @Value("${auth.companyId}")
    String companyId;

    @PostMapping("/page")
    public AjaxResult getAllotListByPage(@RequestBody Map<String, Object> params) {
        params.put("staffId", staffId);
        params.put("companyId", companyId);
        return AjaxResult.back("5200", "获取调拨列表成功")
                .add("pageInfo", allotService.selectByPage(params));
    }

    @PostMapping("/create")
    public AjaxResult createAllotWithDetail(@RequestBody AllotCreate request) throws InterruptedException {
        //从jwt获取到用户，和相关公司code + staffName等
        String staffNo = "S1001", companyCode = "CNY001";
        //获取到当前订单编号
        String code = "DB" + uniqueOrderCode.getCode(companyCode, "ALLOT");
        request.getAllot().setCode(code);
        request.getAllot().setStaffId(staffId);
        request.getAllot().setStaffName("xxx员工");
        request.getAllot().setCompanyId(companyId);
        allotService.createWithItems(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "调拨单创建成功").add("inboundOrderRequest", request);
    }

    @PostMapping("/get/{id}")
    public AjaxResult getAllotWithDetail(@PathVariable String id) {
        return AjaxResult.back(ResultStatus.SUCCESS, "获取调拨单和调拨明细成功！")
                .add("allotRequest", allotService.getAllotWithDetail(id));
    }

    @PostMapping("/modify")
    public AjaxResult modifyAllotWithDetail(@RequestBody AllotModify request) {
        allotService.modifyAllotWithDetail(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "修改调拨单和调拨明细成功！");
    }

    @PostMapping("/auditor")
    public AjaxResult auditorAllotWithDetail(@RequestBody AllotCreate request) {
        allotService.auditorAllotWithDetail(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "审核调拨单和调拨明细成功");
    }
}
